package com.iamteer.day06;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.smartcardio.ATR;
import javax.swing.plaf.metal.OceanTheme;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.Text;
import org.dom4j.io.SAXReader;

public class DOM4J {

	public static void main(String[] args) {
		//1. 创建 SAXReader 对象
		SAXReader reader = new SAXReader();
		try {
		//2. 读取 xml 文档，获取 Document 对象
			Document doc = reader.read(new File("./src/contact.xml"));
			System.out.println(doc);
//			test1(doc);
			
//			test2(doc);
			
//			test3(doc);
			
//			test4(doc);
			
//			test5(doc);

//			test6(doc);

			test7(doc);
		} catch (DocumentException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	public static void test1(Document doc) {
		//得到当前节点下的所有子对象（不包括孙）
		Iterator<Node> iterator = doc.nodeIterator();
		while (iterator.hasNext()) {
			Node node = (Node) iterator.next();
			String name = node.getName();
			System.out.println(name);
			if (node instanceof Element) {	//获取孙对象
				Element e = (Element) node;
				Iterator<Node> iterator2 = e.nodeIterator();
				while (iterator2.hasNext()) {
					Node node2 = (Node) iterator2.next();
					System.out.println(node2.getName());
					System.out.println(">>>");
				}
			}
		}
	}
	
	public static void test2(Document doc) {
		Element root = doc.getRootElement();
		getChildNodes(root);
	}

	public static void getChildNodes(Element element) {
		System.out.println(element.getName());
		
		Iterator<Node> iterator = element.nodeIterator();
		while (iterator.hasNext()) {
			Node node = (Node) iterator.next();
			if (node instanceof Element) {
				Element element2 = (Element) node;
				getChildNodes(element2);
			}
		}
	}
	
	public static void test3(Document doc) {
		//根标签
		Element rootElement = doc.getRootElement();
		//标签名
		String name = rootElement.getName();
		System.out.println(name);
		
		//所有子标签
		List<Element> list = rootElement.elements();
		//传统 for 循环，增强 for 循环，迭代器
/*		for (Element element : list) {
			System.out.println(element.getName());
		}*/
		Iterator<Element> iterator = list.iterator();
		while (iterator.hasNext()) {
			Element element = (Element) iterator.next();
			System.out.println(element.getName());
		}
	}
	
	public static void test4(Document doc) {
		Element contactElement = doc.getRootElement().element("contact");
//		contactElement.attributeValue("id");
/*		List<Attribute> list = contactElement.attributes();
		for (Attribute attribute : list) {
			System.out.println(attribute.getName() + " = " + attribute.getValue());
		}*/
		Iterator<Attribute> it = contactElement.attributeIterator();
		while (it.hasNext()) {
			Attribute attribute = (Attribute) it.next();
			System.out.println(attribute.getName() + "-->" + attribute.getValue());
		}
	}
	
	public static void test5(Document doc) {
		String content = doc.getRootElement().getText();
//		System.out.println("content: " + content);
		
		Element name = doc.getRootElement().element("contact").element("name");
		String text = name.getText();
		System.out.println(text);
		
		Element name2 = doc.getRootElement().element("contact").element("phone");
		String text2 = name2.getText();
		System.out.println(text2);
	}
	
	public static void test6(Document doc) {
		Element rootElement = doc.getRootElement();
		StringBuffer sBuffer = new StringBuffer();
		getChildNodes(rootElement, sBuffer);
		System.out.println(sBuffer.toString());
	}
	//读取完整 xml 文档
	public static void getChildNodes(Element element, StringBuffer sBuffer) {
		//添加根标签左边
		sBuffer.append("<" + element.getName());
		
		//添加属性标签
		List<Attribute> attrs = element.attributes();
		if (attrs != null) {
			for (Attribute attr : attrs) {
				sBuffer.append(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
			}
		}
		//添加根标签右边
		sBuffer.append(">");
		
		Iterator<Node> it = element.nodeIterator();
		while (it.hasNext()) {
			Node node = (Node) it.next();
			
			//标签
			if(node instanceof Element){
				Element element2 = (Element) node;
				getChildNodes(element2, sBuffer);
			}
			
			//文本
			if (node instanceof Text) {
				Text text = (Text) node;
				sBuffer.append(text.getText());
			}
		}
		
		//关闭标签
		sBuffer.append("</" + element.getName() + ">");
	}

	public static void test7(Document doc) {
		List<Contact> list = new ArrayList<Contact>();
		Iterator<Element> it = doc.getRootElement().elementIterator("contact");
		while (it.hasNext()) {
			Element element = (Element) it.next();
			Contact contact = new Contact();
			contact.setId(element.attributeValue("id"));
			contact.setName(element.elementText("name"));
			contact.setAge(element.elementText("age"));
			contact.setEmail(element.elementText("email"));
			contact.setPhone(element.elementText("phone"));
			contact.setQq(element.elementText("qq"));
			list.add(contact);
		}
		
		for (Contact contact : list) {
			System.out.println(contact);
		}
	}
}
